package com.ewhome.apartment.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.ewhome.apartment.R;
import com.ewhome.apartment.di.component.DaggerMainTabComponent;
import com.ewhome.apartment.di.module.MainTabModule;
import com.ewhome.apartment.mvp.contract.MainTabContract;
import com.ewhome.apartment.mvp.presenter.MainTabPresenter;
import com.ewhome.apartment.mvp.ui.fragment.CenterFragment;
import com.ewhome.apartment.mvp.ui.fragment.CommunityFragment;
import com.ewhome.apartment.mvp.ui.fragment.HomeFragment;
import com.ewhome.apartment.mvp.ui.fragment.ServiceFragment;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.jess.arms.base.AdapterViewPager;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class MainTabActivity extends BaseActivity<MainTabPresenter> implements MainTabContract.View {


    @BindView(R.id.bnve)
    BottomNavigationViewEx bnve;
    @BindView(R.id.vp)
    ViewPager vp;
    AdapterViewPager adapterViewPager;
    List<Fragment> fragments=new ArrayList<>();
    private long exitTime = 0;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerMainTabComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .mainTabModule(new MainTabModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_main_tab; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initUI();
    }

    public void initUI(){
        bnve.enableAnimation(true);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);
        fragments.add(HomeFragment.newInstance());
        fragments.add(CommunityFragment.newInstance());
        fragments.add(ServiceFragment.newInstance());
        fragments.add(CenterFragment.newInstance());
        adapterViewPager=new AdapterViewPager(getSupportFragmentManager(),fragments);
        vp.setAdapter(adapterViewPager);
        bnve.setupWithViewPager(vp);
    }
    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit(){
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ArmsUtils.makeText(this,"再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            ArmsUtils.exitApp();
        }
    }
}
